package org.example.type;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @Author: luyg
 * @Date: 2024/11/11/9:07
 * @Description:
 */
public class TypeAliasRegistry {

    private Map<String,Class<?>> TYPE_ALIASES = new HashMap<>();

    public TypeAliasRegistry(){
        // 构造函数里注册系统内置的类型别名
        registerAlias("string", String.class);
        // 基本包装类型
        registerAlias("byte", Byte.class);
        registerAlias("long", Long.class);
        registerAlias("short", Short.class);
        registerAlias("int", Integer.class);
        registerAlias("integer", Integer.class);
        registerAlias("double", Double.class);
        registerAlias("float", Float.class);
        registerAlias("boolean", Boolean.class);

    }

    public void registerAlias(String key,Class<?> value){
        String lowerCase = key.toLowerCase(Locale.ENGLISH);
        TYPE_ALIASES.put(lowerCase,value);
    }

    public <T> Class<T> resolveAlias(String key) {
        String ke = key.toLowerCase(Locale.ENGLISH);
        return (Class<T>) TYPE_ALIASES.get(ke);
    }

}
